<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            left: 0px;

        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        // 事件  ： 1.鼠标按下 ： onmousedown  2.鼠标移动 onmousemove 3.鼠标抬起 onmouseup

        var boxEle = document.querySelector(".box");
        boxEle.onmousedown = function (e) {
            console.log("鼠标按下了A");
            var x = e.clientX - boxEle.offsetLeft;
            var y = e.clientY - boxEle.offsetTop;
            boxEle.onmousemove = function (e) {
                console.log("拖动了");
                console.log("鼠标抬起B");
                var xx = e.clientX;
                var yy = e.clientY;
                var resx =  xx - x  ; // 绿色的线；
                var resy = yy - y;
                boxEle.style.left = resx + "px";
                boxEle.style.top = resy + "px";
            }
            boxEle.onmouseup = function(e){
                // 鼠标抬起的时候 移动事件解除；
                boxEle.onmousemove = "";
            }
        }




    </script>
</body>

</html>